<script setup lang="ts">
import { ref } from 'vue';

interface Props {
  modelValue: number;
  tabsOptions: {
    tab2: string[];
    tab3: string[];
    tab4: string[];
  };
  dataTotal: string;
}
withDefaults(defineProps<Props>(), {
  modelValue: 0,
  tabsOptions: () => ({
    tab2: [],
    tab3: [],
    tab4: [],
  }),
  dataTotal: '',
});

const emit = defineEmits<{
  (e: 'update:model-value', value: number): void;
  (e: 'onChange'): void;
}>();

const translateX = ref(5);

const handleClick = (e: any, value: number) => {
  const { clientWidth, offsetLeft } = e.target;
  translateX.value = offsetLeft - (65 - clientWidth) / 2;
  emit('update:model-value', value);
  emit('onChange');
};
</script>
<template>
  <div class="slideTabs">
    <div class="main">
      <div class="tabs flex justify-between items-center text-[#7A7A7A] text-xs h-full w-full">
        <div
          v-for="(text, index) in tabsOptions.tab2"
          :key="index"
          @click="handleClick($event, index + 1)"
          :class="{ 'text-white': modelValue === index + 1 }"
        >
          {{ text }}
        </div>
      </div>
      <div class="slidingBlock" :style="{ transform: `translateX(${translateX}px)` }"></div>
    </div>
    <div class="flex items-center justify-between text-xs text-[#999999] mt-1">
      <div
        v-for="(text, index) in tabsOptions.tab3"
        :key="index"
        :class="{ 'text-[#EC2D69]': modelValue === index + 1 }"
      >
        {{ text }}
      </div>
    </div>
    <div
      class="mt-3 w-full bg-[#fbeff3] rounded-lg text-center leading-[36px] text-[#EC2D69] text-[13px] relative"
      v-show="dataTotal"
    >
      {{ dataTotal }}
      <div
        class="w-[10px] h-[10px] absolute top-[-5px] left-1/2 bg-[#fbeff3] rounded"
        style="transform: rotate(45deg)"
      ></div>
    </div>
  </div>
</template>
<style lang="less" scoped>
.slideTabs {
  width: 100%;

  .main {
    height: 30px;
    border-radius: 15px;
    background: #f6f7f8;
    position: relative;
  }

  .tabs {
    position: absolute;
    z-index: 2;
    padding-left: 24px;
    padding-right: 18px;
    & > div {
      cursor: pointer;
    }
  }
  .slidingBlock {
    width: 65px;
    height: 24px;
    background: #ec2d69;
    border-radius: 12px;
    position: absolute;
    z-index: 1;
    top: 3px;
    transition: transform 0.3s;
  }
}
</style>
